﻿using Vision001.Base;
using Vision001.Helper;
using Vision001.Models;
using Vision001.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Common;

namespace Vision001.ViewModels
{
    public class CollectDataViewModel : NotifyPorperChanged
    {
        private CollectDateView _collectDateView;
        public static CancellationTokenSource _TokenSource;
        private List<DataEntryModel> _datasList;

        public CollectDataViewModel(CollectDateView collectDateView)
        {
            _datasList = new List<DataEntryModel>();
            _collectDateView = collectDateView;
            Cards = new ObservableCollection<ObservableCollection<CollectDataModel>>();
            Card1 = new ObservableCollection<CollectDataModel>();
            Card2 = new ObservableCollection<CollectDataModel>();
            Card3 = new ObservableCollection<CollectDataModel>();
            Card4 = new ObservableCollection<CollectDataModel>();
            Card5 = new ObservableCollection<CollectDataModel>();
            Card6 = new ObservableCollection<CollectDataModel>();
            Card7 = new ObservableCollection<CollectDataModel>();
            Card8 = new ObservableCollection<CollectDataModel>();
            Card9 = new ObservableCollection<CollectDataModel>();
            Card10 = new ObservableCollection<CollectDataModel>();
            Card11 = new ObservableCollection<CollectDataModel>();
            Card12 = new ObservableCollection<CollectDataModel>();
            init();
            WindowLoadedCommand = new Command() { ExeAction = new Action<object>(WindowLoadedcmd) };
            WindowClosingCommand = new Command() { ExeAction = new Action<object>(WindowClosingcmd) };
            StartCollectCommand = new Command() { ExeAction = new Action<object>(StartCollectCmd) };
            StopCollectCommand = new Command() { ExeAction = new Action<object>(StopCollectCmd) };
            SaveToCsvCollectCommand = new Command() { ExeAction = new Action<object>(SaveToCsvCollectCmd) };
            PowerSetCurrentCommand = new Command() { ExeAction = new Action<object>(PowerSetCurrentCmd) };
            PowerSetVoltageCommand = new Command() { ExeAction = new Action<object>(PowerSetVoltageCmd) };
            PowerGetCurrentCommand = new Command() { ExeAction = new Action<object>(PowerGetCurrentCmd) };
            PowerGetVoltageCommand = new Command() { ExeAction = new Action<object>(PowerGetVoltageCmd) };
        }

        #region 初始化命令和参数

        private string _powerSetCurrent;

        public string PowerSetCurrent
        {
            set
            {
                _powerSetCurrent = value; this.SetProperty();
            }
            get
            {
                return _powerSetCurrent;
            }
        }

        private string _powerSetVoltage;

        public string PowerSetVoltage
        {
            set
            {
                _powerSetVoltage = value; this.SetProperty();
            }
            get
            {
                return _powerSetVoltage;
            }
        }

        private string _powerGetCurrent;

        public string PowerGetCurrent
        {
            set
            {
                _powerGetCurrent = value;
                this.SetProperty();
            }
            get
            {
                return _powerGetCurrent;
            }
        }

        private string _powerGetVoltage;

        public string PowerGetVoltage
        {
            set
            {
                _powerGetVoltage = value; this.SetProperty();
            }
            get
            {
                return _powerGetVoltage;
            }
        }

        private int _powerSetChannel;

        public int PowerSetChannel
        {
            set
            {
                _powerSetChannel = value; this.SetProperty();
            }
            get
            {
                return _powerSetChannel;
            }
        }

        private int _powerGetChannel;

        public int PowerGetChannel
        {
            set
            {
                _powerGetChannel = value; this.SetProperty();
            }
            get
            {
                return _powerGetChannel;
            }
        }

        private Command _powerSetCurrentCommand;

        /// <summary>
        /// 设置电流方法
        /// </summary>
        public Command PowerSetCurrentCommand
        {
            set
            {
                _powerSetCurrentCommand = value;
            }
            get
            {
                return _powerSetCurrentCommand;
            }
        }

        private Command _powerSetVoltageCommand;

        /// <summary>
        /// 设置电压方法
        /// </summary>
        public Command PowerSetVoltageCommand
        {
            set
            {
                _powerSetVoltageCommand = value;
            }
            get
            {
                return _powerSetVoltageCommand;
            }
        }

        private Command _powerGetCurrentCommand;

        /// <summary>
        /// 读取电流方法
        /// </summary>
        public Command PowerGetCurrentCommand
        {
            set
            {
                _powerGetCurrentCommand = value;
            }
            get
            {
                return _powerGetCurrentCommand;
            }
        }

        private Command _powerGetVoltageCommand;

        /// <summary>
        /// 读取电压方法
        /// </summary>
        public Command PowerGetVoltageCommand
        {
            set
            {
                _powerGetVoltageCommand = value;
            }
            get
            {
                return _powerGetVoltageCommand;
            }
        }

        private Command _startcollectCommand;

        /// <summary>
        /// 开始采集
        /// </summary>
        public Command StartCollectCommand
        {
            set
            {
                _startcollectCommand = value;
            }
            get
            {
                return _startcollectCommand;
            }
        }

        private Command _stopcollectCommand;

        /// <summary>
        /// 停止采集
        /// </summary>
        public Command StopCollectCommand
        {
            set
            {
                _stopcollectCommand = value;
            }
            get
            {
                return _stopcollectCommand;
            }
        }

        private Command _savetocsvcollectCommand;

        /// <summary>
        /// 保存到csv文件
        /// </summary>
        public Command SaveToCsvCollectCommand
        {
            set
            {
                _savetocsvcollectCommand = value;
            }
            get
            {
                return _savetocsvcollectCommand;
            }
        }

        private Command _windowLoadedCommand;

        public Command WindowLoadedCommand
        {
            set
            {
                _windowLoadedCommand = value;
            }
            get
            {
                return _windowLoadedCommand;
            }
        }

        private Command _windowClosingCommand;

        public Command WindowClosingCommand
        {
            set
            {
                _windowClosingCommand = value;
            }
            get
            {
                return _windowClosingCommand;
            }
        }

        private ObservableCollection<ObservableCollection<CollectDataModel>> _cards;

        /// <summary>
        /// 所有的电压采集卡
        /// </summary>
        public ObservableCollection<ObservableCollection<CollectDataModel>> Cards
        {
            set
            {
                _cards = value;
                this.SetProperty();
            }
            get
            {
                return _cards;
            }
        }

        private ObservableCollection<CollectDataModel> _card1;

        /// <summary>
        /// 电压采集卡1
        /// </summary>
        public ObservableCollection<CollectDataModel> Card1
        {
            set
            {
                _card1 = value;
                this.SetProperty();
            }
            get
            {
                return _card1;
            }
        }

        private ObservableCollection<CollectDataModel> _card2;

        /// <summary>
        /// 电压采集卡2
        /// </summary>
        public ObservableCollection<CollectDataModel> Card2
        {
            set
            {
                _card2 = value;
                this.SetProperty();
            }
            get
            {
                return _card2;
            }
        }

        private ObservableCollection<CollectDataModel> _card3;

        /// <summary>
        /// 电压采集卡3
        /// </summary>
        public ObservableCollection<CollectDataModel> Card3
        {
            set
            {
                _card3 = value;
                this.SetProperty();
            }
            get
            {
                return _card3;
            }
        }

        private ObservableCollection<CollectDataModel> _card4;

        /// <summary>
        /// 电压采集卡4
        /// </summary>
        public ObservableCollection<CollectDataModel> Card4
        {
            set
            {
                _card4 = value;
                this.SetProperty();
            }
            get
            {
                return _card4;
            }
        }

        private ObservableCollection<CollectDataModel> _card5;

        /// <summary>
        /// 电压采集卡5
        /// </summary>
        public ObservableCollection<CollectDataModel> Card5
        {
            set
            {
                _card5 = value;
                this.SetProperty();
            }
            get
            {
                return _card5;
            }
        }

        private ObservableCollection<CollectDataModel> _card6;

        /// <summary>
        /// 电压采集卡6
        /// </summary>
        public ObservableCollection<CollectDataModel> Card6
        {
            set
            {
                _card6 = value;
                this.SetProperty();
            }
            get
            {
                return _card6;
            }
        }

        private ObservableCollection<CollectDataModel> _card7;

        /// <summary>
        /// 电压采集卡7
        /// </summary>
        public ObservableCollection<CollectDataModel> Card7
        {
            set
            {
                _card7 = value;
                this.SetProperty();
            }
            get
            {
                return _card7;
            }
        }

        private ObservableCollection<CollectDataModel> _card8;

        /// <summary>
        /// 电压采集卡8
        /// </summary>
        public ObservableCollection<CollectDataModel> Card8
        {
            set
            {
                _card8 = value;
                this.SetProperty();
            }
            get
            {
                return _card8;
            }
        }

        private ObservableCollection<CollectDataModel> _card9;

        /// <summary>
        /// 电压采集卡9
        /// </summary>
        public ObservableCollection<CollectDataModel> Card9
        {
            set
            {
                _card9 = value;
                this.SetProperty();
            }
            get
            {
                return _card9;
            }
        }

        private ObservableCollection<CollectDataModel> _card10;

        /// <summary>
        /// 电压采集卡10
        /// </summary>
        public ObservableCollection<CollectDataModel> Card10
        {
            set
            {
                _card10 = value;
                this.SetProperty();
            }
            get
            {
                return _card10;
            }
        }

        private ObservableCollection<CollectDataModel> _card11;

        /// <summary>
        /// 电压采集卡11
        /// </summary>
        public ObservableCollection<CollectDataModel> Card11
        {
            set
            {
                _card11 = value;
                this.SetProperty();
            }
            get
            {
                return _card11;
            }
        }

        private ObservableCollection<CollectDataModel> _card12;

        /// <summary>
        /// 电压采集卡12
        /// </summary>
        public ObservableCollection<CollectDataModel> Card12
        {
            set
            {
                _card12 = value;
                this.SetProperty();
            }
            get
            {
                return _card12;
            }
        }

        #endregion 初始化命令和参数

        #region 绑定方法

        /// <summary>
        /// 获取电压
        /// </summary>
        /// <param name="obj"></param>
        private void PowerGetVoltageCmd(object obj)
        {
            int value = PowerGetChannel + 1;
            Global._It6302.SetChannel(value);
            PowerGetVoltage = Global._It6302.GetVoltage(value).ToString();
        }

        /// <summary>
        /// 获取电流
        /// </summary>
        /// <param name="obj"></param>
        private void PowerGetCurrentCmd(object obj)
        {
            int value = PowerGetChannel + 1;
            Global._It6302.SetChannel(value);
            PowerGetCurrent = Global._It6302.GetCurrent(value).ToString();
        }

        /// <summary>
        /// 设置电压
        /// </summary>
        /// <param name="obj"></param>
        private void PowerSetVoltageCmd(object obj)
        {
            if (PowerSetVoltage == null || PowerSetVoltage == "")
            {
                return;
            }
            double value = Convert.ToDouble(PowerSetVoltage);
            Global._It6302.SetChannel(PowerSetChannel + 1);
            Global._It6302.SetVoltage(PowerSetChannel + 1, value);
        }

        /// <summary>
        /// 设置电流
        /// </summary>
        /// <param name="obj"></param>
        private void PowerSetCurrentCmd(object obj)
        {
            if (PowerSetCurrent == null || PowerSetCurrent == "")
            {
                return;
            }
            double value = Convert.ToDouble(PowerSetCurrent);
            Global._It6302.SetChannel(PowerSetChannel + 1);
            Global._It6302.SetCurrent(PowerSetChannel + 1, value);
        }

        /// <summary>
        /// 结果保存至CSV文件
        /// </summary>
        /// <param name="obj"></param>
        private void SaveToCsvCollectCmd(object obj)
        {
        }

        /// <summary>
        /// 关闭采集
        /// </summary>
        /// <param name="obj"></param>
        private void StopCollectCmd(object obj)
        {
            _TokenSource.Cancel();
        }

        /// <summary>
        /// 开始采集
        /// </summary>
        /// <param name="obj"></param>
        private async void StartCollectCmd(object obj)
        {
            try
            {
                _datasList = new List<DataEntryModel>();
                int index = Convert.ToInt32(obj) - 1;
                _TokenSource.Cancel();
                await Task.Delay(1600);
                _TokenSource = new CancellationTokenSource();

                //执行读取
                Task.Run(async () =>
                {
                    while (!_TokenSource.IsCancellationRequested)
                    {
                        List<double> value = Global._ZsAnalogInputs[index].GetValue();

                        if (value.Count > 7)
                        {
                            DataEntryModel data = new DataEntryModel();
                            data.index1 = value[0];

                            data.index2 = value[1];

                            data.index3 = value[2];
                            data.index4 = value[3];
                            data.index5 = value[4];
                            data.index6 = value[5];
                            data.index7 = value[6];
                            data.index8 = value[7];
                            _datasList.Add(data);
                            for (int i = 0; i < Cards[index].Count; i++)
                            {
                                Cards[index][i].Index = value[i];
                            }
                        }
                        if (_datasList.Count > 500) { _datasList.RemoveAt(0); }
                        await Task.Delay(300);
                    }
                }, _TokenSource.Token);
            }
            catch (Exception e)
            {
                Mylog.Error(e);
            }
        }

        /// <summary>
        /// 界面退出方法
        /// </summary>
        private void WindowClosingcmd(object obj)
        {
            _TokenSource.Cancel();
        }

        /// <summary>
        /// 界面加载方法
        /// </summary>
        /// <param name="obj"></param>
        private void WindowLoadedcmd(object obj)
        {
            _TokenSource = new CancellationTokenSource();
            Global._It6302.SetOutputStatus(1, true);
            Global._It6302.SetOutputStatus(2, true);
        }

        private void init()
        {
            Card1 = new ObservableCollection<CollectDataModel>();
            Card2 = new ObservableCollection<CollectDataModel>();
            Card3 = new ObservableCollection<CollectDataModel>();
            Card4 = new ObservableCollection<CollectDataModel>();
            Card5 = new ObservableCollection<CollectDataModel>();
            Card6 = new ObservableCollection<CollectDataModel>();
            Card7 = new ObservableCollection<CollectDataModel>();
            Card8 = new ObservableCollection<CollectDataModel>();
            Card9 = new ObservableCollection<CollectDataModel>();
            Card10 = new ObservableCollection<CollectDataModel>();
            Card11 = new ObservableCollection<CollectDataModel>();
            Card12 = new ObservableCollection<CollectDataModel>();

            for (int i = 0; i < 8; i++)
            {
                Card1.Add(new CollectDataModel() { IndexName = "通道：" + (i + 1).ToString(), Index = 0 });
                Card2.Add(new CollectDataModel() { IndexName = "通道：" + (i + 1).ToString(), Index = 0 });
                Card3.Add(new CollectDataModel() { IndexName = "通道：" + (i + 1).ToString(), Index = 0 });
                Card4.Add(new CollectDataModel() { IndexName = "通道：" + (i + 1).ToString(), Index = 0 });
                Card5.Add(new CollectDataModel() { IndexName = "通道：" + (i + 1).ToString(), Index = 0 });
                Card6.Add(new CollectDataModel() { IndexName = "通道：" + (i + 1).ToString(), Index = 0 });
                Card7.Add(new CollectDataModel() { IndexName = "通道：" + (i + 1).ToString(), Index = 0 });
                Card8.Add(new CollectDataModel() { IndexName = "通道：" + (i + 1).ToString(), Index = 0 });
                Card9.Add(new CollectDataModel() { IndexName = "通道：" + (i + 1).ToString(), Index = 0 });
                Card10.Add(new CollectDataModel() { IndexName = "通道：" + (i + 1).ToString(), Index = 0 });
                Card11.Add(new CollectDataModel() { IndexName = "通道：" + (i + 1).ToString(), Index = 0 });
                Card12.Add(new CollectDataModel() { IndexName = "通道：" + (i + 1).ToString(), Index = 0 });
            }

            Cards.Add(Card1);
            Cards.Add(Card2);
            Cards.Add(Card3);
            Cards.Add(Card4);
            Cards.Add(Card5);
            Cards.Add(Card6);
            Cards.Add(Card7);
            Cards.Add(Card8);
            Cards.Add(Card9);
            Cards.Add(Card10);
            Cards.Add(Card11);
            Cards.Add(Card12);
        }

        #endregion 绑定方法
    }
}